package com.ruoyi.common.gscUtil.StudyUtil.SocketUtil;

import java.io.*;
import java.net.*;

public class Server {
    public static void main(String[] args) {
        int port = 12345; // 服务器监听的端口号

        try (ServerSocket serverSocket = new ServerSocket(port)) {
            System.out.println("服务器已启动，等待客户端连接...");

            while (true) {
                Socket clientSocket = serverSocket.accept(); // 等待客户端连接
                System.out.println("客户端已连接: " + clientSocket.getInetAddress());

                // 获取输入输出流
                BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
                PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);

                // 读取客户端数据
                String clientMessage = in.readLine();
                System.out.println("收到客户端消息: " + clientMessage);

                // 发送响应
                out.println("服务器响应: " + clientMessage);

                // 关闭连接
                clientSocket.close();
                System.out.println("客户端连接已关闭");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}